﻿#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QWheelEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QList>

class MyGraphicsView : public QGraphicsView {
    Q_OBJECT
    Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged)
public:
    enum Mode{
        Select,
        DrawLine, //线
        DrawStraightLine, //直线
        DrawRect, //矩形
        DrawRound, // 圆
        DrawArrow, //箭头
        DrawArrow2, //折线箭头
        DrawText //文本
    };

    MyGraphicsView(QWidget* parent = nullptr);

    void doScale(qreal s  = 1.0);

    QGraphicsItem* addText(QPoint scenePos,QString txt,QFont font);

    QGraphicsItem *currentItem() const;
    void setCurrentItem(QGraphicsItem *currentItem);

    Mode mode() const;
    void setMode(Mode mode);

    int penSize() const;
    void setPenSize(int penSize);

    QColor penColor() const;
    void setPenColor(const QColor &penColor);

    QRect getRect(QPoint p1,QPoint p2);

    void undo();

    bool canUndo() const
    {
        return m_canUndo;
    }

signals:
    void doubleClickedItem(QGraphicsItem* item);

    void canUndoChanged(bool canUndo);

protected:
    void wheelEvent(QWheelEvent* event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent*event) override;
    void mouseDoubleClickEvent(QMouseEvent* event) override;

private:
    Mode m_mode = Select;
    int m_penSize = 1;
    bool m_isPressed = false;
    QColor m_penColor = Qt::red;
    QPoint m_pressPos;
    QGraphicsItem* m_currentItem = nullptr;
    QList<QGraphicsItem*> m_items;
    bool m_canUndo = false;
};

#endif // MYGRAPHICSVIEW_H
